const express = require('express');
const path = require('path');  // 确保引入了path模块
const userRoutes = require('./routes/userRoutes');
const thingRoutes = require('./routes/thingRoutes');
const ArticleRoutes = require('./routes/ArticleRoutes');
const AIRoutes = require('./routes/AIRoutes');
const OrderRoutes = require('./routes/OrderRoutes');
const AgriRoutes = require('./routes/AgriRoutes');
const cors = require('cors');
const { verifyToken } = require('./middleware/auth'); // 引入验证中间件

const app = express();

// 配置CORS中间件
app.use(cors({
    origin: "*",
    credentials: true,
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Authorization'],
    exposedHeaders: ['Authorization'],
    maxAge: 86400
}));

app.use(express.urlencoded({ extended: true }));
// 解析JSON数据
app.use(express.json());

// 测试路由
app.post("/hello", async(req, res) => {
    res.send("Hello World");
});

// 使用路由模块
app.use('/user', userRoutes);
// 之前的路由，可以参考写其它内容
app.use('/thing', verifyToken, thingRoutes); // 为thingRoutes添加验证中间件
app.use('/article', verifyToken, ArticleRoutes); // 为ArticleRoutes添加验证中间件
app.use('/ai', verifyToken, AIRoutes);
app.use('/orders', verifyToken, OrderRoutes); // 为OrderRoutes添加验证中间件
app.use('/agri_products',verifyToken, AgriRoutes); // 为AgriRoutes添加验证中间件

// 配置 public/images 作为静态文件目录
app.use('/images', express.static(path.join(__dirname, '../public/images')));


app.listen(8080, () => {
    console.log("Server is running on port 8080");
});